<!-- 学生编辑弹窗 -->
<template>
  <el-dialog
    :title="isUpdate?'修改学生':'添加学生'"
    :visible="visible"
    width="550px"
    :destroy-on-close="true"
    :lock-scroll="false"
    @update:visible="updateVisible">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="100px">
      
      <el-form-item label="头像:">
        <uploadImage :limit="1" v-model="form.avatar"></uploadImage>
      </el-form-item>
                        
      <el-form-item
        label="姓名:"
        prop="name">
        <el-input
          :maxlength="20"
          v-model="form.name"
          placeholder="请输入姓名"
          clearable/>
      </el-form-item>
                              
      <el-form-item label="性别:" prop="gender">
        <el-select
          clearable
          class="ele-block"
          v-model="form.gender"
          placeholder="请选择性别">
          <el-option label="男" :value="1"/>
          <el-option label="女" :value="2"/>
          <el-option label="保密" :value="3"/>
        </el-select>
      </el-form-item>
      
      <el-form-item
        label="学籍号:"
        prop="id_card">
        <el-input
          :maxlength="20"
          v-model="form.id_card"
          placeholder="请输入学籍号"
          clearable/>
      </el-form-item>
                              
      <el-form-item label="学校名称:" prop="school_id">
        <el-select
          filterable
          clearable
          v-model="form.school_id"
          @change="getGradeList"
          size="small"
          placeholder="-请选择学校-"
          class="ele-block">
          <el-option v-for="item in schoolList" :key="item.id" :label="item.name" :value="item.id"/>
        </el-select>
      </el-form-item>
      
      <el-form-item label="年级名称:" prop="grade_id">
        <el-select
          filterable
          clearable
          v-model="form.grade_id"
          @change="getClassList"
          size="small"
          placeholder="-请选择年级-"
          class="ele-block">
          <el-option v-for="item in gradeList" :key="item.id" :label="item.name" :value="item.id"/>
        </el-select>
      </el-form-item>
                              
     <el-form-item label="班级名称:" prop="class_id">
        <el-select
          filterable
          clearable
          v-model="form.class_id"
          @change="$forceUpdate()"
          size="small"
          placeholder="-请选择班级-"
          class="ele-block">
          <el-option v-for="item in classList" :key="item.id" :label="item.name" :value="item.id"/>
        </el-select>
      </el-form-item>
                              
      <el-form-item
        label="电话号码:"
        prop="phone">
        <el-input
          :maxlength="20"
          v-model="form.phone"
          placeholder="请输入电话号码"
          clearable/>
      </el-form-item>
                      
      <el-form-item label="状态:">
        <el-radio-group
          v-model="form.status">
                  <el-radio :label="1">正常</el-radio>
                  <el-radio :label="2">停用</el-radio>
                </el-radio-group>
      </el-form-item>
                              
    </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button
        type="primary"
        @click="save"
        :loading="loading">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import uploadImage from '@/components/uploadImage'

export default {
  name: 'StudentEdit',
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  components: {uploadImage},
  data() {
    return {
      // 表单数据
      form: Object.assign({status:1}, this.data),
      // 表单验证规则
      rules: {
              avatar: [
          {required: true, message: '请输入头像', trigger: 'blur'}
        ],
                  name: [
          {required: true, message: '请输入姓名', trigger: 'blur'}
        ],
                  id_card: [
          {required: true, message: '请输入学籍号', trigger: 'blur'}
        ],
                  gender: [
          {required: true, message: '请输入性别:0保密 1男 2女', trigger: 'blur'}
        ],
                  school_id: [
          {required: true, message: '请选择学校', trigger: 'blur'}
        ],
                  grade_id: [
          {required: true, message: '请选择年级', trigger: 'blur'}
        ],
                  class_id: [
          {required: true, message: '请选择班级', trigger: 'blur'}
        ],
        //           phone: [
        //   {required: true, message: '请输入电话号码', trigger: 'blur'}
        // ],
                  
        status: [
          {required: true, message: '请选择状态', trigger: 'blur'}
        ]
            },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
      // 学校列表
      schoolList: [],
      // 年级列表
      gradeList: [],
      // 班级列表
      classList: [],
    };
  },
   mounted() {
    this.getSchoolList(); // 查询学校列表
    if(this.form.school_id){
        this.getGradeList();
        console.log('getGradeList');
        if(this.form.grade_id){
            this.getClassList();
             console.log('getClassList');
        }
    }
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
        this.isUpdate = true;
      } else {
        this.form = {};
        this.isUpdate = false;
      }
    }
  },
  methods: {
    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$http.post('/student/edit', this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    },
    /**
     * 获取学校列表
     */
    getSchoolList() {
      this.$http.get('/school/getSchoolList').then(res => {
        if (res.data.code === 0) {
          this.schoolList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
    /**
     * 获取年级列表
     */
    getGradeList() {
       
      if(this.form.school_id){
            this.form.grade_id = null;
            this.form.class_id = null;
      }  
        
      this.$http.post('/grade/getGradeList',{oid:this.form.school_id}).then(res => {
        if (res.data.code === 0) {
          this.gradeList = res.data.data;
          this.$forceUpdate();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
    /**
     * 获取班级列表
     */
    getClassList() {
      if(this.form.school_id && this.form.grade_id){
        this.form.class_id = null;
      }  
        
      this.$http.post('/clazz/getClazzList',{gid:this.form.grade_id}).then(res => {
        if (res.data.code === 0) {
          this.classList = res.data.data;
          this.$forceUpdate();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
  }
}
</script>

<style scoped>
</style>
